package rs.frenjoynet.core.ui.util;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.ScopeType;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: ppopovic
 * Date: 29.10.2009.
 * Time: 12.27.42
 * To change this template use File | Settings | File Templates.
 */
@Name ("skinBean")
@Scope (ScopeType.EVENT)
@Install (false)
public class SkinBean
{
	private String skin;
	private static final String cookieParam = "skin";
	private static final String defaultSkin = "blueSky";

	public String getSkin()
	{
		if (skin != null)
			return skin;

		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();

		Cookie[] cookies = request.getCookies();
		if (cookies != null)
		{
			for (Cookie cookie : cookies)
			{
				if (cookie.getName().equals(cookieParam))
				{
					skin = cookie.getValue();
				}

			}
		}

		if (skin == null)
		{
			Cookie skinCookie = new Cookie(cookieParam, defaultSkin);
			skinCookie.setMaxAge(365);
			HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();
			response.addCookie(skinCookie);
			skin = defaultSkin;
		}

		return skin;
	}

	public void setSkin(String newSkin)
	{
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();

		Cookie skinCookie = new Cookie(cookieParam, newSkin);
		skinCookie.setMaxAge(365);
		response.addCookie(skinCookie);
		this.skin = newSkin;
	}

}
